package com.atguigu.gulimall.product.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrRelationVo;
import com.atguigu.gulimall.product.vo.AttrgroupWithattrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author dy
 * @email 1091330089@qq.com
 * @date 2022-08-11 11:06:25
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelation;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;


    //product/attrgroup/{catelogId}/withattr  获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithattrByCategoryId(@PathVariable("catelogId") Long catelogId){
        List<AttrgroupWithattrVo> attrgroupWithattrVos = attrGroupService.getAttrgroupWithattrByCategoryId(catelogId);
        return R.ok().put("data",attrgroupWithattrVos);
    }






    //product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        //获取属性分组 所关联的全部属性。
        List<AttrEntity> list = attrAttrgroupRelation.getRelationAttr(attrGroupId);
        return R.ok().put("data",list);
    }

    //product/attrgroup/{attrgroupId}/noattr/relation
    //获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoattrRelation(@PathVariable("attrgroupId") Long attrGroupId,@RequestParam Map<String, Object> params){
        //获取属性分组里面还没有关联的本分类里面的其他基本属性
        PageUtils page = attrService.getNoattrRelation(attrGroupId,params);

        return R.ok().put("page",page);
    }

    //product/attrgroup/attr/relation  添加属性与分组关联关系
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVos){
        attrAttrgroupRelation.addAttrRelation(attrRelationVos);
        return R.ok();
    }







    //product/attrgroup
    //删除属性与分组 的关联关系
    @PostMapping("/attr/relation/delete")
    public R  deleteAttrAttrgroup(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroup){
        attrAttrgroupRelation.deleteAttrAttrgroup(attrAttrgroup);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();

        //查找catelogId 的完整路径
        Long[] path = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
